package com.maimang.platform.controller.system;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.entity.SysMenu;
import com.maimang.platform.common.entity.SysRole;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.common.service.SysMenuService;
import com.maimang.platform.common.utils.ShiroUtils;
import com.maimang.platform.lang.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @author Rocky
 * @version 1.0.0
 * @类名 SysMenuController
 * @描述 <br>菜单信息</br>
 * @日期 2019-03-24
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("/system/menu")
@Api(value = "菜单信息", description = "配置系统各个模块菜单显示信息")
public class SysMenuController extends BaseController {
	private String prefix = "system/menu";

	@Autowired
	private SysMenuService menuService;

	@RequiresPermissions("system:menu:view")
	@GetMapping()
	public String menu() {
		return prefix + "/menu";
	}

	@RequiresPermissions("system:menu:list")
	@GetMapping("/list")
	@ResponseBody
	public List<SysMenu> list(SysMenu menu) {
		List<SysMenu> menuList = menuService.selectMenuList(menu);
		return menuList;
	}

	/**
	 * 删除菜单
	 */
	@Log(title = "菜单管理", businessType = BusinessType.DELETE)
	@ApiOperation(value = "删除菜单", notes = "根据menuId删除菜单，若存在子菜单或菜单已分配则不允许操作")
	@RequiresPermissions("system:menu:remove")
	@PostMapping("/remove/{menuId}")
	@ResponseBody
	public Result remove(@PathVariable("menuId") Long menuId) {
		if (menuService.selectCountMenuByParentId(menuId) > 0) {
			return error(1, "存在子菜单,不允许删除");
		}
		if (menuService.selectCountRoleMenuByMenuId(menuId) > 0) {
			return error(1, "菜单已分配,不允许删除");
		}
		ShiroUtils.clearCachedAuthorizationInfo();
		return toResult(menuService.deleteMenuById(menuId));
	}

	/**
	 * 新增
	 */
	@GetMapping("/add/{parentId}")
	public String add(@PathVariable("parentId") Long parentId, ModelMap modelMap) {
		SysMenu menu = null;
		if (0L != parentId) {
			menu = menuService.selectMenuById(parentId);
		} else {
			menu = new SysMenu();
			menu.setMenuId(0L);
			menu.setMenuName("主目录");
		}
		modelMap.put("menu", menu);
		return prefix + "/add";
	}

	/**
	 * 新增保存菜单
	 */
	@Log(title = "菜单管理", businessType = BusinessType.INSERT)
	@ApiOperation(value = "添加菜单", notes = "添加菜单并保存信息到菜单权限表")
	@RequiresPermissions("system:menu:add")
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(SysMenu menu) {
		menu.setCreateUser(ShiroUtils.getLoginName());
		ShiroUtils.clearCachedAuthorizationInfo();
		return toResult(menuService.insertMenu(menu));
	}

	/**
	 * 修改菜单
	 */
	@GetMapping("/edit/{menuId}")
	public String edit(@PathVariable("menuId") Long menuId, ModelMap modelMap) {
		modelMap.put("menu", menuService.selectMenuById(menuId));
		return prefix + "/edit";
	}

	/**
	 * 修改保存菜单
	 */
	@Log(title = "菜单管理", businessType = BusinessType.UPDATE)
	@ApiOperation(value = "修改菜单", notes = "修改并保存菜单信息")
	@RequiresPermissions("system:menu:edit")
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(SysMenu menu) {
		// 如果没有登录或session过期
		if (StringUtils.isEmpty(ShiroUtils.getLoginName())) {
			redirect("login");
		}
		menu.setUpdateUser(ShiroUtils.getLoginName());
		ShiroUtils.clearCachedAuthorizationInfo();
		return toResult(menuService.updateMenu(menu));
	}

	/**
	 * 选择菜单图标
	 */
	@GetMapping("/icon")
	public String icon() {
		return prefix + "/icon";
	}

	/**
	 * 校验菜单名称
	 */
	@PostMapping("/checkMenuNameUnique")
	@ApiOperation(value = "校验菜单", notes = "校验菜单信息是否唯一")
	@ResponseBody
	public String checkMenuNameUnique(SysMenu menu) {
		return menuService.checkMenuNameUnique(menu);
	}

	/**
	 * 加载角色菜单列表树
	 */
	@GetMapping("/roleMenuTreeData")
	@ResponseBody
	public List<Map<String, Object>> roleMenuTreeData(SysRole role) {
		List<Map<String, Object>> tree = menuService.roleMenuTreeData(role);
		return tree;
	}

	/**
	 * 加载所有菜单列表树
	 */
	@GetMapping("/menuTreeData")
	@ResponseBody
	public List<Map<String, Object>> menuTreeData(SysRole role) {
		List<Map<String, Object>> tree = menuService.menuTreeData();
		return tree;
	}

	/**
	 * 选择菜单树
	 */
	@GetMapping("/selectMenuTree/{menuId}")
	public String selectMenuTree(@PathVariable("menuId") Long menuId, ModelMap modelMap) {
		modelMap.put("menu", menuService.selectMenuById(menuId));
		return prefix + "/tree";
	}
}